package arithmetic.LeetCode;

import java.util.HashMap;
import java.util.Map;

/**
 * 128. 最长连续序列
 * <p>
 * 给定一个未排序的整数数组 nums ，找出数字连续的最长序列（不要求序列元素在原数组中连续）的长度。
 * <p>
 * 请你设计并实现时间复杂度为 O(n) 的算法解决此问题。
 * <p>
 * <p>
 * 示例 1：
 * 输入：nums = [100,4,200,1,3,2]
 * 输出：4
 * 解释：最长数字连续序列是 [1, 2, 3, 4]。它的长度为 4。
 * <p>
 * 示例 2：
 * 输入：nums = [0,3,7,2,5,8,4,6,0,1]
 * 输出：9
 * <p>
 * https://leetcode.cn/problems/longest-consecutive-sequence
 *
 * @author jiangfeng on 2023/4/24
 */
public class LongestConsecutive {
    public static void main(String[] args) {
        System.out.println(new Solution().longestConsecutive(new int[]{100,4,200,1,3,2}));
        System.out.println(new Solution().longestConsecutive(new int[] {0, 3, 7, 2, 5, 8, 4, 6, 0, 1}));
    }


   static class Solution {
       //2.使用并查集
       public int longestConsecutive(int[] nums){
           if(nums.length<2){
               return nums.length;
           }

           HashMap<Integer,Integer> map =new HashMap(nums.length);
           UnionFind uf =  new UnionFind(nums.length);
           for(int i=0;i<nums.length;i++){
               map.put(nums[i],i);
           }


           for(int i=0;i<nums.length;i++){
               Integer v = map.get(nums[i]+1);
               if(v!=null){
                   uf.union(i,v);
               }
           }
           // 找最大值
           int res= 0;
           for(int i=0;i<nums.length;i++){
               res=Math.max(res,nums[i]-nums[uf.findFather(i)]);
           }
           return res;

       }

       class UnionFind{
           int[] data;
           // init
           public UnionFind(int size){
               data=new int[size];
               for(int i=0;i<size;i++){
                   //初始化每个节点的父节点是自己
                   data[i]=i;
               }
           }
           // 返回父节点
           public int union(int fatherIndex,int sonIndex){
               int a =findFather(fatherIndex);
               int b =findFather(sonIndex);
               if(a!=b){
                   data[b]=a;
               }
               return a;
           }
           public int findFather(int index){
               // 压缩一下优化一下,所有节点父节点 都存根节点.
               if(data[index]!=index){
                   data[index] = findFather(data[index]);
               }
               return data[index];
           }
       }
   }
    static class Solution3 {
        //1.一个hash搞定
        public int longestConsecutive(int[] nums) {
            if (nums.length < 2) {
                return nums.length;
            }
            int res = 0;
            //
            Map<Integer, Integer> map = new HashMap(nums.length);

            for (int i = 0; i < nums.length; i++) {
                map.put(nums[i], -1);
            }
            for (int i = 0; i < nums.length; i++) {
                res = Math.max(res, nums[i]-findMinAndSet(map, nums[i], 0) + 1);
            }
            return res;

        }

        // 经过的路径都标记一下.
        public int findMinAndSet(Map<Integer, Integer> map, int key, int step) {
            Integer v = map.get(key - step);
            // 到了顶,返回自己,上次递归传入的值.
            if (v == null) {
                return key+1-step;
            }

            // 还没计算最左边界.
            if (v == -1) {
                int minLeft = findMinAndSet(map, key, step + 1);
                // map v存储当前元素离最左边界元素的距离,返回一个最左边界元素
                map.put(key - step, key - step - minLeft);
                return minLeft;
            } else {
                // 已经计算过了,直接返回最左元素就行.
                return key -step - v;
            }
        }
    }

    static class Solution2 {
        public int longestConsecutive(int[] nums) {
            // 使用并查集. init,find,uni;
            if (nums.length < 2) {
                return nums.length;
            }
            int res = 0;
            // 1.init

            int[] ifu = new int[nums.length];
            // k是数,v 数组位置.
            Map<Integer, Integer> map = new HashMap(nums.length);
            for (int i = 0; i < ifu.length; i++) {
                ifu[i] = i;
                map.put(nums[i], i);
            }


            // 2.uni
            for (int i = 0; i < nums.length; i++) {
                Integer father = map.get(nums[i] - 1);
                if (father != null) {
                    uni(ifu, father, i);
                }
            }
            for (int i = 0; i < nums.length; i++) {
                res = Math.max(nums[i] - nums[findFather(ifu, i)] + 1, res);
            }
            return res;

        }

        // 带压缩路径
        public int findFather(int[] ifu, int i) {
            int father = ifu[i];
            if (ifu[i] != i) {
                ifu[i] = findFather(ifu, ifu[i]);
            }
            return father;
        }

        public void uni(int[] ifu, int father, int son) {
            int root = findFather(ifu, father);
            int root2 = findFather(ifu, son);
            if (root == root2) {
                return;
            }
            ifu[son] = root;

        }


    }
}
